'use strict'

define(['jquery', 'qiniuPlayer', 'bootstrap', 'select2', 'shiftform',
    'shiftform', 'validate', 'validate.en', 'plupload', 'qiniu'], function($,
        QPlayer) {
  return {
    init: function() {
      this.initEdit();
      this.initSearch();
      this.initVideo();
      this.initAuth();
      this.initCloses();
      this.initAfresh();
    },

    initEdit: function() {

      $(document).on('shown.bs.modal', '#edit', function(e) {

        $(document).find('form[data-validate="true"]').bt_validate() // bind
        // validate

        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });

    },

    initAfresh: function() {
      $(document).on('shown.bs.modal', '#afresh', function(e) {
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },

    initCloses: function() {
      var that = this;
      $(document).on('shown.bs.modal', '#closes', function(e) {
        var afterVedio = document.getElementById("afterVedio").value;
        that.player = new QPlayer({
          url: afterVedio + '?avvod/m3u8/s/960x640/vb/1000k',
          container: document.getElementById("videoPlayer"),
          defaultViewConfig: {
            showControls: true,
          }
        });
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },
    initAuth: function() {
      var that = this;
      $(document).on('shown.bs.modal', '#auth', function(e) {
        var beforeVedio = document.getElementById("beforeVedio").value;
        that.player = new QPlayer({
          url: beforeVedio + '?avvod/m3u8/s/960x640/vb/1000k',
          container: document.getElementById("videoPlayer"),
          defaultViewConfig: {
            showControls: true,
          }
        });
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },

    initVideo: function() {
      var that = this;
      $(document).on('shown.bs.modal', '#video', function(e) {
        var beforeVedio = document.getElementById("beforeVedio").value;
        that.player = new QPlayer({
          url: window.__CTX__.cloudVideo + "/" + beforeVedio + '?avvod/m3u8/s/960x640/vb/1000k',
          container: document.getElementById("videoPlayer"),
          defaultViewConfig: {
            showControls: true,
          }
        });
        $(document).find('form[data-validate="true"]').bt_validate() // bind
        // validate
        var $form = $(document).find("form") // bind shiftform
        $form.shiftform({
          beforeSubmit: function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
    },
    initSearch: function() {
      $(document).on("shown.bs.modal", "#search", function(e) {
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate() //bind validate
      });
    }
  }
})
